# -*- coding: UTF-8 -*-

"""Utility command for remote Pyrc control"""
import time

def say(pyrc,umask,dest,message):
    pyrc.msg(dest[0],message[0])

def esegui(pyrc,umask,dest,message):
    pyrc._raw(message[0])

def send_msg(pyrc,umask,dest,message):
    pyrc.msg(message[0],message[1])
    
def send_notice(pyrc,umask,dest,message):
    pyrc.notice(message[0],message[1])
    
def change_nick(pyrc,umask,dest,message):
    pyrc.setnick(message[0])
    
def parti(pyrc,umask,dest,message):
    pyrc.part(dest[0],message[1])
    
def entra(pyrc,umask,dest,message):
    pyrc.join(message[0])
        
def esci(pyrc,umask,dest,message):
    quitmsg = "Closing Connection"
    if message[0]:
        quitmsg = message[0]
    pyrc.quit(quitmsg)
    pyrc.closed = True

def reconnect(pyrc,umask,dest,message):
    pyrc.reconnect()

def jump(pyrc,umask,dest,message):
    newserver = message[0]
    pyrc.quit("Changing to %s..." % newserver)
    pyrc.host = newserver
    pyrc.connect()
    
def togglelog(pyrc,umask,dest,message):
    pyrc.log = not pyrc.log
    pyrc.msg(dest[0],"Logging is %s" % str(pyrc.log))
    
def op(pyrc,umask,dest,message):
    action = message[2]
    if action is None:
        who = umask[0]
    else:
        who = action 
    pyrc.mode(dest[0],"+o %s" % who)
    
def deop(pyrc,umask,dest,message):
    action = message[2]
    if action is None:
        who = umask[0]
    else:
        who = action 
    pyrc.mode(dest[0],"-o %s" % who)

def whoareyou(pyrc,umask,dest,message):
    pyrc.msg(dest[0],"I am %s" % pyrc.me)
    
def getuser(pyrc,umask,dest,message):
    pyrc.msg(dest[0],str(pyrc.getchan(dest[0]).users))
def getevents(pyrc,umask,dest,message):
    for e in pyrc.events:
        time.sleep(1)
        pyrc.msg(dest[0],str(e))
    
events = [
    ['PRIVMSG', '(hukk)!.+','(.+)','!do (.+)', esegui],
    ['PRIVMSG', '(hukk)!.+','(.+)','!say (.+)', say],
    ['PRIVMSG', '(hukk)!.+','(.+)','!msg ([^ ]+) (.+)', send_msg],
    ['PRIVMSG', '(hukk)!.+','(.+)','!notice ([^ ]+) (.+)', send_notice],
    ['PRIVMSG', '(hukk)!.+','(.+)','!nick ([^ ]+)( .+)?', change_nick],
    ['PRIVMSG', '(hukk)!.+','(.+)','!join (#[^ ]+)$', entra],
    ['PRIVMSG', '(hukk)!.+','(.+)','!part( (.+))?$', parti],
    ['PRIVMSG', '(hukk)!.+','(.+)','!quit( .+)?', esci],
    ['PRIVMSG', '(hukk)!.+','(.+)','!reconnect( .+)?', reconnect],
    ['PRIVMSG', '(hukk)!.+','(.+)','!jump (.+)', jump],
    ['PRIVMSG', '(hukk)!.+','(.+)','(!log)$', togglelog],
    ['PRIVMSG', '(hukk)!.+','(#.+)','(!op)( ([^ ]+))?$',op],
    ['PRIVMSG', '(hukk)!.+','(#.+)','(!deop)( ([^ ]+))?$',deop],
    ['PRIVMSG', '(hukk)!.+','(.+)','(!wru)$',whoareyou],
    ['PRIVMSG', '(hukk)!.+','(#.+)','(!getusers)$', getuser],
    ['PRIVMSG', '(hukk)!.+','(#.+)','(!events)$', getevents],
]

